﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using WaterRevenueMS.Core.Repository.BasicSet;
using WaterRevenueMS.Componnent.Entities.BasicSet;
using WaterRevenueMS.Componnent.Common;
using WaterRevenueMS.Domain.ViewModels.BasicSet.MeterState;

namespace WaterRevenueMS.Core.Service.BasicSet.Impl
{
    [Export(typeof(IMeterStateService))]
    public class MeterStateService : CoreServiceBase, IMeterStateService
    {
        [Import]
        public IMeterStateRepository MeterStateRepository { get; set; }

        public IQueryable<MeterState> MeterStates
        {
            get { return MeterStateRepository.Entities; }
        }

        public OperationResult Insert(MeterStateModel model)
        {
            var entity = new MeterState
            {
                Id = model.Id,
                Yi_BZT = model.Yi_BZT,
                Zhuang_TDM = model.Zhuang_TDM,
                CreateDT = DateTime.Now,
                ReviseDT = DateTime.Now,
                OrderNo = model.OrderNo
            };
            MeterStateRepository.Insert(entity);
            return new OperationResult(OperationResultType.Success, "添加成功");
        }

        public OperationResult Update(MeterStateModel model)
        {
            var entity = MeterStateRepository.Entities.FirstOrDefault(t => t.Id == model.Id);
            if (entity != null)
            {
                entity.Yi_BZT = model.Yi_BZT;
                entity.Zhuang_TDM = model.Zhuang_TDM;
                entity.OrderNo = model.OrderNo;
            }
            MeterStateRepository.Update(entity);
            return new OperationResult(OperationResultType.Success, "更新成功");
        }

        public OperationResult Delete(int Id)
        {
            var entity = MeterStateRepository.Entities.FirstOrDefault(t => t.Id == Id);
            if (entity != null)
            {
                MeterStateRepository.Delete(entity);
            }
            return new OperationResult(OperationResultType.Success, "删除成功");
        }
    }
}
